﻿namespace DCHealthCheck.MvcWeb.Controllers {

    #region Usings
    using DCHealthCheck.Common;
    using DCHealthCheck.DomainModel;
    using DCHealthCheck.MvcWeb.Classes;
    using DCHealthCheck.MvcWeb.Classes.DataServices;
    using DCHealthCheck.MvcWeb.Classes.ViewModels;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    #endregion

    public class ProcessesController : BaseController {

        private readonly IProcessInfoDataService ProcessInfoDataService;
        private readonly IRepository<DCHealthCheck.DomainModel.ProcessInfo, IEntityManager> ProcessInfoRepository;

        public ProcessesController(
            IProcessInfoDataService processInfoDataService,
            IRepository<DCHealthCheck.DomainModel.ProcessInfo, IEntityManager> processInfoRepository) {
            if (processInfoDataService == null) {
                throw new ArgumentNullException("processInfoDataService");
            }
            if (processInfoRepository == null) {
                throw new ArgumentNullException("processInfoRepository");
            }

            this.ControllerName = "Processes";
            this.ProcessInfoDataService = processInfoDataService;
            this.ProcessInfoRepository = processInfoRepository;
        }

        public ActionResult Details(ProcessInfoViewModel viewModel) {
            this.ProcessInfoDataService.GetDetails(viewModel);
            return View(viewModel);
        }

        [EnableCompression]
        [HttpPost]
        public FileContentResult Download(ProcessInfoViewModel viewModel, string token) {

            string fileName = string.Format("HealthCheckProcesses_{0}.csv", DateTime.UtcNow.ToString("yyyy-MM-dd-HHmmss"));
            string fileData = this.ProcessInfoDataService.GetCSVData(viewModel);

            HttpContext.Response.AppendCookie(new HttpCookie("fileDownloadToken", token));
            return File(fileData.ToByteArray(Encoding.UTF8), "application/octet-stream", fileName);
        }

        public ActionResult Index(ProcessInfoViewModel viewModel) {
            this.ProcessInfoDataService.ProcessData(viewModel);
            return View(viewModel);
        }

    }
}
